package org.rsbot.loader.script;

import java.io.PrintStream;
import org.rsbot.loader.asm.Label;
import org.rsbot.loader.asm.MethodVisitor;

public class CodeReader
{
  private final Buffer code;

  public CodeReader(byte[] paramArrayOfByte)
  {
    this.code = new Buffer(paramArrayOfByte);
  }

  public void accept(MethodVisitor paramMethodVisitor) {
    int i = this.code.g2();

    Label[] arrayOfLabel1 = new Label[this.code.g1()];
    int j = 0; for (int k = arrayOfLabel1.length; j < k; j++) {
      arrayOfLabel1[j] = new Label();
    }
    while (i-- > 0) {
      j = this.code.g1();
      if (j == 1) {
        paramMethodVisitor.visitInsn(this.code.g1());
      } else if (j == 2) {
        paramMethodVisitor.visitIntInsn(this.code.g1(), this.code.g2());
      } else if (j == 3) {
        paramMethodVisitor.visitVarInsn(this.code.g1(), this.code.g1());
      } else if (j == 4) {
        paramMethodVisitor.visitTypeInsn(this.code.g1(), this.code.gstr());
      }
      else
      {
        Object localObject;
        if (j == 5) {
          k = this.code.g1();
          String str1 = this.code.gstr();
          localObject = this.code.gstr();
          String str2 = this.code.gstr();
          System.out.println(str1 + "." + (String)localObject + " " + str2);
          paramMethodVisitor.visitFieldInsn(k, str1, (String)localObject, str2);
        } else if (j == 6) {
          paramMethodVisitor.visitMethodInsn(this.code.g1(), this.code.gstr(), this.code.gstr(), this.code.gstr());
        }
        else if (j == 7) {
          paramMethodVisitor.visitJumpInsn(this.code.g1(), arrayOfLabel1[this.code.g1()]);
        } else if (j == 8) {
          k = this.code.g1();
          if (k == 1)
            paramMethodVisitor.visitLdcInsn(Integer.valueOf(this.code.g4()));
          else if (k == 2)
            paramMethodVisitor.visitLdcInsn(Float.valueOf(Float.parseFloat(this.code.gstr())));
          else if (k == 3)
            paramMethodVisitor.visitLdcInsn(Long.valueOf(this.code.g8()));
          else if (k == 4)
            paramMethodVisitor.visitLdcInsn(Double.valueOf(Double.parseDouble(this.code.gstr())));
          else if (k == 5)
            paramMethodVisitor.visitLdcInsn(this.code.gstr());
        }
        else if (j == 9) {
          paramMethodVisitor.visitIincInsn(this.code.g1(), this.code.g1());
        }
        else
        {
          int m;
          if (j == 10) {
            k = this.code.g2();
            m = this.code.g2();
            localObject = arrayOfLabel1[this.code.g1()];
            int i1 = this.code.g1(); int i2 = 0;
            Label[] arrayOfLabel3 = new Label[i1];
            while (i2 < i1) {
              arrayOfLabel3[(i2++)] = arrayOfLabel1[this.code.g1()];
            }
            paramMethodVisitor.visitTableSwitchInsn(k, m, (Label)localObject, arrayOfLabel3);
          } else if (j == 11) {
            Label localLabel = arrayOfLabel1[this.code.g1()];
            m = this.code.g1(); int n = 0;
            int[] arrayOfInt = new int[m];
            while (n < m) {
              arrayOfInt[(n++)] = this.code.g2();
            }
            m = this.code.g1();
            n = 0;
            Label[] arrayOfLabel2 = new Label[m];
            while (n < m) {
              arrayOfLabel2[(n++)] = arrayOfLabel1[this.code.g1()];
            }
            paramMethodVisitor.visitLookupSwitchInsn(localLabel, arrayOfInt, arrayOfLabel2);
          } else if (j == 12) {
            paramMethodVisitor.visitMultiANewArrayInsn(this.code.gstr(), this.code.g1());
          } else if (j == 13) {
            paramMethodVisitor.visitTryCatchBlock(arrayOfLabel1[this.code.g1()], arrayOfLabel1[this.code.g1()], arrayOfLabel1[this.code.g1()], this.code.gstr());
          }
          else if (j == 14) {
            paramMethodVisitor.visitLocalVariable(this.code.gstr(), this.code.gstr(), this.code.gstr(), arrayOfLabel1[this.code.g1()], arrayOfLabel1[this.code.g1()], this.code.g1());
          }
          else if (j == 15) {
            paramMethodVisitor.visitLabel(arrayOfLabel1[this.code.g1()]);
          }
        }
      }
    }
  }

  static abstract interface Opcodes
  {
    public static final int INSN = 1;
    public static final int INT_INSN = 2;
    public static final int VAR_INSN = 3;
    public static final int TYPE_INSN = 4;
    public static final int FIELD_INSN = 5;
    public static final int METHOD_INSN = 6;
    public static final int JUMP_INSN = 7;
    public static final int LDC_INSN = 8;
    public static final int IINC_INSN = 9;
    public static final int TABLESWITCH_INSN = 10;
    public static final int LOOKUPSWITCH_INSN = 11;
    public static final int MULTIANEWARRAY_INSN = 12;
    public static final int TRY_CATCH_BLOCK = 13;
    public static final int LOCAL_VARIABLE = 14;
    public static final int LABEL = 15;
  }
}